package org.example.nebula.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.example.nebula.common.utils.NebulaUtil;
import org.example.nebula.entity.input.SummaryInput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: jszhang
 * @date: 2025/10/27-9:07
 */
@Slf4j
@Service
public class SpaceService {

    @Autowired
    private CommonService commonService;

    public List<String> getAllSpace() {
        List<String> spaceNames = new ArrayList<>();
        JSONArray jsonArray = commonService.executeJson( "show spaces");
        log.info(jsonArray.toString());
        JSONArray data = jsonArray.getJSONObject(0).getJSONArray("data");
        for (int i = 0; i < data.size(); i++) {
            String name = data.getJSONObject(i).getJSONArray("row").getStr(0);
            spaceNames.add(name);
        }
        return spaceNames;
    }

    public JSONObject getSummary(SummaryInput summaryInput) {
        log.info(summaryInput.toString());

        /*
        获取图的统计信息
         */
        JSONArray jsonArray = commonService.executeJson(
                NebulaUtil.spaceStatsInfo(summaryInput.getSpaceName())
        );
//        log.info(jsonArray.toString());
        JSONArray data = jsonArray.getJSONObject(0).getJSONArray("data");

        JSONObject jsonObject = new JSONObject(true);
        if (CollectionUtil.isEmpty(data)) {
            jsonObject.set("Entities", new JSONArray());
            jsonObject.set("Relations", new JSONArray());
            jsonObject.set("TotalCount", new JSONObject());
        }


        /*
        获取索引信息
         */
        JSONArray indexInfo = commonService.executeJson(
                NebulaUtil.getTagIndexInfo(summaryInput.getSpaceName())
        );
        Map<String, List<String>> indexMap = new HashMap<>();
        JSONArray indexArray = indexInfo.getJSONObject(0).getJSONArray("data");
        for (int m = 0; m < indexArray.size(); m++) {
            JSONArray indexRow = indexArray.getJSONObject(m).getJSONArray("row");
            List<String> propertyList = indexMap.getOrDefault(indexRow.getStr(1), new ArrayList<>());
            propertyList.add(indexRow.getJSONArray(2).join(",").replaceAll("\"", ""));
            indexMap.put(indexRow.getStr(1), propertyList);
        }

        JSONArray entityArray = new JSONArray();
        JSONObject relationJSONObject = new JSONObject(true);
        JSONObject countJSONObject = new JSONObject(true);
        for (int i = 0; i < data.size(); i++) {
            JSONArray row = data.getJSONObject(i).getJSONArray("row");
            if (row.getStr(0).equals("Tag")) {
                JSONObject tagInfo = new JSONObject();
                tagInfo.set("EntityType", row.getStr(1));
                tagInfo.set("EntityCount", row.getStr(2));

                /*
                获取节点的属性信息
                 */
                JSONObject tagProperty = new JSONObject(true);
                JSONArray propertyInfo = commonService.executeJson(
                        NebulaUtil.getPropertyInfo(summaryInput.getSpaceName(), row.getStr(1))
                );

                JSONArray propertyData = propertyInfo.getJSONObject(0).getJSONArray("data");
                for (int k = 0; k < propertyData.size(); k++) {
                    JSONArray propertyRow = propertyData.getJSONObject(k).getJSONArray("row");
                    tagProperty.set(propertyRow.getStr(0), propertyRow.getStr(1));
                }
                tagProperty.set("id", "string");
                tagInfo.set("Properties", tagProperty);

                List<String> indexList = indexMap.get(row.getStr(1));
                if (indexList != null) {
                    tagInfo.set("Index", indexList);
                } else {
                    tagInfo.set("Index", new JSONArray());
                }

                entityArray.add(tagInfo);
            } else if (row.getStr(0).equals("Edge")) {
                JSONObject edgeInfo = new JSONObject(true);

                edgeInfo.set("Count", row.getStr(2));
                relationJSONObject.set(row.getStr(1), edgeInfo);
            } else if (row.getStr(0).equals("Space")) {
                if (row.getStr(1).equals("vertices")) {
                    countJSONObject.set("Entities", row.getStr(2));
                } else if (row.getStr(1).equals("edges")) {
                    countJSONObject.set("Relations", row.getStr(2));
                }
            }
        }

        jsonObject.set("Entities", entityArray);
        jsonObject.set("Relations", relationJSONObject);
        jsonObject.set("TotalCount", countJSONObject);

        return jsonObject;
    }

}
